#include<func.h>
typedef struct Client_s
{
    int netfd;   //连接的哪个netfd
    int isAlive; //存活条件
    time_t lastActive; //时间戳

}Clinet_t;

int main(int argc,char*argv[])
{
    //server
    // ./epoll_liaotian2 192.168.213.128 3333
    ARGS_CHECK(argc,3);
    int sockfd = socket(AF_INET,SOCK_STREAM,0);
    //ready bind
    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(atoi(argv[2]));
    addr.sin_addr.s_addr = inet_addr(argv[1]);
    //bind
    int flags = 1;
    int ret = setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&flags,sizeof(flags));
    ret = bind(sockfd,(struct sockaddr*)&addr,sizeof(addr));
    ERROR_CHECK(ret,-1,"bind");
    //listen
    listen(sockfd,100);//half list ready
    //ready epoll and accept
    int epfd = epoll_create(10);//create epfd
    struct epoll_event event; //epoll_event is struct
    event.events = EPOLLIN;
    event.data.fd = sockfd;
    //epoll_ctl
    //int readyset[10]; 这里用户态就绪队列写错le 
    struct epoll_event readyset[10];
    char buf[4096];
    //net放在外面
    //int netfd = -1;
    //create Client netfd
    Clinet_t Client[1024];
    for(int i =0;i<1024;i++)
    {
        Client[i].isAlive = 0;
        Client[i].netfd = -1;
    }
    int num = 0; //记录个数


    epoll_ctl(epfd,EPOLL_CTL_ADD,sockfd,&event);//add sockfd
    //buf
    while(1)
    {
       //准备好时间结构体
       //struct timeval timeout;
       //timeout.tv_sec = 1;
       //timeout.tv_usec = 0;
       int readynum = epoll_wait(epfd,readyset,10,1000);
       time_t curtime = time(NULL);
       printf("curtime = %s\n",ctime(&curtime));//打印每次的时间

       //int readynum = epoll_wait(epfd,readyset,10,-1);//先不写超时 -1才是永久超时
       for(int i=0;i<readynum;i++)
       {
            if(readyset[i].data.fd == sockfd)
            {
                int netfd = accept(sockfd,NULL,NULL);//暂时不获取客户端地址
                Client[num].netfd = netfd;
                Client[num].isAlive = 1;
                printf("Num = %d, netfd = %d\n", num, Client[num].netfd);
                //增加监听
                event.events = EPOLLIN;
                event.data.fd = Client[num].netfd;
                epoll_ctl(epfd,EPOLL_CTL_ADD,netfd,&event); // add netfd
                Client[num].lastActive = time(NULL);
                //num ++
                num++;
                event.events = EPOLLIN;
                event.data.fd = STDIN_FILENO;
                epoll_ctl(epfd,EPOLL_CTL_ADD,STDIN_FILENO,&event); //add stdin
                //epoll_ctl(epfd,EPOLL_CTL_DEL,sockfd,NULL);// 移除对sockfd的监听
            }
            for(int j =0; j<num; j++)
            {
                //i  等于  j里面的某一个值，说明在num队列里面有一个值是就绪了
                if(Client[j].isAlive !=0 && (readyset[i].data.fd == Client[j].netfd))
                {
                    //netfd 就绪
                    printf("netfd = %d\n",Client[j].netfd);
                    
                    bzero(buf,sizeof(buf));
                    ssize_t sret = recv(Client[j].netfd,buf,sizeof(buf),0);
                    if(sret == 0)
                    {
                        //断开连接
                        Client[j].isAlive = 0;
                        close(Client[j].netfd);
                        Client[j].netfd = -1;
                        epoll_ctl(epfd,EPOLL_CTL_DEL,Client[j].netfd,NULL);
                        continue; //跳出监听
                    }
                    // 写成i了 写j
                    Client[j].lastActive = time(NULL);
                    //转发给其他人
                    for(int k = 0;k<num;k++)
                    {
                        if( j==k || Client[k].isAlive == 0 )
                        {
                            continue;
                        }
                        send(Client[k].netfd,buf,sizeof(buf),0);
                    }
                }
            }

       }
       for(int j=0;j<num;j++)
       {
            if(Client[j].isAlive !=0 && curtime - Client[j].lastActive >= 10)
                {
                    printf("timout id = %d, Client= %d\n ",j,Client[j].netfd);
                    close(Client[j].netfd);
                    epoll_ctl(epfd,EPOLL_CTL_DEL,Client[j].netfd,NULL);
                    Client[j].isAlive = 0;
                }
       }
    }

    close(sockfd);
    return 0;
}